//
//  HuYu_ZhiBoViewController.m
//  mhkjjsd
//
//  Created by 凤利史 on 2018/5/25.
//  Copyright © 2018年 sunrose. All rights reserved.
//

#import "HuYu_ZhiBoViewController.h"
#import "FXDanmaku.h"
#import "NSObject+FXAlertView.h"
#import "DemoDanmakuItemData.h"
#import "DemoDanmakuItem.h"
#import "HuYu_ZhiBoCollectionViewCell.h"

#define CurrentDevice [UIDevice currentDevice]
#define CurrentOrientation [[UIDevice currentDevice] orientation]
#define ScreenScale [UIScreen mainScreen].scale
#define NotificationCetner [NSNotificationCenter defaultCenter]


@interface HuYu_ZhiBoViewController ()<FXDanmakuDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

Strong FXDanmaku *danmaku;
@property (strong, nonatomic) NSTimer *timer;
@property (nonatomic,strong) UICollectionView *huYuCollectionView;
@end

@implementation HuYu_ZhiBoViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self selector:@selector(timerAction) userInfo:nil repeats:true];
   
}
- (void)timerAction{
    
    [self addDatasWithCount:3];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    
    self.timer = nil;
    
    [self.timer invalidate];
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    
    [self setupDanmaku];
    
    // Do any additional setup after loading the view.
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Device Orientation
- (BOOL)shouldAutorotate {
    return YES;
}

#pragma mark - Views
- (void)setupDanmaku {
    
    self.danmaku = [[FXDanmaku alloc] initWithFrame:CGRectMake(0, 100, DEF_SCREEN_WIDTH, 200)];
    [self.view addSubview:self.danmaku];
    FXDanmakuConfiguration *config = [FXDanmakuConfiguration defaultConfiguration];
    config.rowHeight = [DemoDanmakuItem itemHeight];
    self.danmaku.configuration = config;
    self.danmaku.backgroundColor = [UIColor clearColor];
    self.danmaku.delegate = self;
    [self.danmaku registerNib:[UINib nibWithNibName:NSStringFromClass([DemoDanmakuItem class]) bundle:nil]
       forItemReuseIdentifier:[DemoDanmakuItem reuseIdentifier]];
}
- (void)creationView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //    layout.itemSize = CGSizeMake(screenScale(290), screenScale(290));
    //    layout.minimumLineSpacing = screenScale(20);
    //    layout.minimumInteritemSpacing = screenScale(20);
    
    _huYuCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 49) collectionViewLayout:layout];
    _huYuCollectionView.showsVerticalScrollIndicator = NO;
    _huYuCollectionView.delegate = self;
    _huYuCollectionView.dataSource = self;
    _huYuCollectionView.backgroundColor = getColor(@"F5F7FA");
    
    [_huYuCollectionView registerClass:[HuYu_ZhiBoCollectionViewCell class] forCellWithReuseIdentifier:@"collectIdentifier"];
    
//    [_huYuCollectionView registerNib:[UINib nibWithNibName:@"HYRootCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    [_huYuCollectionView registerNib:[UINib nibWithNibName:@"HuYu_ZhiBoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HuYu_ZhiBoCollectionViewCell"];
//    [_huYuCollectionView registerNib:[UINib nibWithNibName:@"HYRootFirstCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HYRootFirstCollectionViewCell"];
    \
    [self.view addSubview:_huYuCollectionView];
    
    UIButton *bianJiBtn = [[UIButton alloc] init];
    [self.view addSubview:bianJiBtn];
    [bianJiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).with.offset(-(KIsiPhoneX?24:0)-20);
        make.right.equalTo(self.view).with.offset(-15);
        
    }];
    [bianJiBtn setBackgroundImage:[UIImage imageNamed:@"dynamic_live_issued_btn"] forState:UIControlStateNormal];
    
    
    
}
#pragma mark --- UICollectionViewDelegate ---
//确定section的个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{
    
    return 1;
    
}

//设置每个 UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return CGSizeMake((DEF_SCREEN_WIDTH-10)/2, 175);
    
    
}

//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
   
    
    return UIEdgeInsetsMake(5, 0, 0, 0);//上、左、下、右
    
    
}

//定义每个UICollectionView 的横向间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}


-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    

    
    NSLog(@"看看是什么%ld", (long)indexPath.row);
        
        
   
    
}

#pragma mark --- UICollectionViewDataSource ---
//确定每个section对应的item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 12;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
 
    
    
    
    HuYu_ZhiBoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HuYu_ZhiBoCollectionViewCell" forIndexPath:indexPath];
    
    return cell;
        
        
  
    
}


//#pragma mark - Observer
//- (void)setupObserver {
//    [NotificationCetner addObserver:self
//                           selector:@selector(applicationWillResignActive:)
//                               name:UIApplicationWillResignActiveNotification
//                             object:nil];
//    [NotificationCetner addObserver:self
//                           selector:@selector(applicationDidBecomeActive:)
//                               name:UIApplicationDidBecomeActiveNotification
//                             object:nil];
//}
//
//- (void)applicationDidBecomeActive:(NSNotification *)notification {
//    [self.danmaku start];
//}
//
//- (void)applicationWillResignActive:(NSNotification*)notification {
//    [self.danmaku pause];
//}

#pragma mark - FXDanmakuDelegate
- (void)danmaku:(FXDanmaku *)danmaku didClickItem:(FXDanmakuItem *)item withData:(DemoDanmakuItemData *)data {
//    [self fx_presentConfirmViewWithTitle:nil
//                                 message:[NSString stringWithFormat:@"You click %@", data.desc]
//                      confirmButtonTitle:nil
//                       cancelButtonTitle:@"Ok"
//                          confirmHandler:nil
//                           cancelHandler:nil];
    
}

#pragma mark - DataSource
- (void)addDatasWithCount:(NSUInteger)count {
    NSUInteger index = 0;
    for (NSUInteger i = 0; i < count; i++) {
        DemoDanmakuItemData *data = [DemoDanmakuItemData data];
        data.avatarName = [NSString stringWithFormat:@"avatar%d", arc4random()%6];
        data.desc = [NSString stringWithFormat:@"阿三老师4月28日三点公开课直播-%@", @(index++)];
        [self.danmaku addData:data];
    }
    
    if (!self.danmaku.isRunning) {
        [self.danmaku start];
    }
}

#pragma mark - Orientation
#pragma mark For iOS8 And Later
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    // if danmaku view's height will change in different device orientation, you'd better pause danmaku and clean screen before orientation will change.
    [self.danmaku pause];
    // if you could set danmaku.cleanScreenWhenPaused = false, then you need to call 'cleanScreen' method after pause.
    //    [self.danmaku cleanScreen];
    
    [coordinator animateAlongsideTransition:nil
                                 completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
                                     // resume danmaku after orientation did change
                                     [self.danmaku start];
                                 }];
}
#pragma mark For Version below iOS8
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self.danmaku pause];
    // if you could set danmaku.cleanScreenWhenPaused = false, then you need to call 'cleanScreen' method after pause.
    //    [self.danmaku cleanScreen];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self.danmaku start];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
